home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / unixcpio.gz / unixnet.cpio / ttydriv.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  3KB  |  178 lines

  1. /* TTY input driver */
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include "config.h"
  5.  
  6. #define    NULLCHAR    (char *)0;
  7. int ttymode;
  8. #define    TTY_LIT    0        /* Send next char literally */
  9. #define    TTY_RAW    1
  10. #define TTY_COOKED    2
  11.  
  12. int ttyecho=1;
  13. #define    TTY_NOECHO    0
  14. #define    TTY_ECHO    1
  15.  
  16. #ifdef    FLOW
  17. int ttyflow=1;
  18. #endif
  19.  
  20. #define    LINESIZE    256
  21.  
  22. #define CTLR    18
  23. #define    CTLU    21
  24. #define    CTLV    22
  25. #define    CTLW    23
  26. #define    CTLZ    26
  27. #define    RUBOUT    127
  28.  
  29. raw()
  30. {
  31.     ttymode = TTY_RAW;
  32. #ifdef    ATARI_ST
  33.     set_stdout(ttymode);    /* CR/LF vs LF madness...  -- hyc */
  34. #endif
  35. }
  36.  
  37. cooked()
  38. {
  39.     ttymode = TTY_COOKED;
  40. #ifdef    ATARI_ST
  41.     set_stdout(ttymode);
  42. #endif
  43. }
  44.  
  45. void
  46. echo()
  47. {
  48.     ttyecho = TTY_ECHO;
  49. }
  50.  
  51. void
  52. noecho()
  53. {
  54.     ttyecho = TTY_NOECHO;
  55. }
  56.  
  57. /* Accept characters from the incoming tty buffer and process them
  58.  * (if in cooked mode) or just pass them directly (if in raw mode).
  59.  * Returns the number of characters available for use; if non-zero,
  60.  * also stashes a pointer to the character(s) in the "buf" argument.
  61.  */
  62.  /*Control-R added by df for retype of lines - useful in Telnet */
  63.  /*Then df got impatient and added Control-W for erasing words  */
  64.  /* Control-V for the literal-next function, slightly improved
  65.   * flow control, local echo stuff -- hyc */
  66. int
  67. ttydriv(c,buf)
  68. char c;
  69. char **buf;
  70. {
  71.     static char linebuf[LINESIZE];
  72.     static char *cp = linebuf;
  73.     char *rp ;
  74.     int cnt;
  75.     int seenprint;
  76.  
  77.     if(buf == (char **)NULL)
  78.         return 0;    /* paranoia check */
  79.  
  80.     cnt = 0;
  81.     switch(ttymode){
  82.     case TTY_LIT:
  83.         ttymode = TTY_COOKED;    /* Reset to cooked mode */
  84.     case TTY_RAW:
  85.         *cp++ = c;
  86.         cnt = cp - linebuf;
  87.         cp = linebuf;
  88.         break;
  89.     case TTY_COOKED:
  90.         /* Perform cooked-mode line editing */
  91. #ifdef PC9801
  92.         switch(c){
  93. #else
  94.         switch(c & 0x7f){
  95. #endif
  96.         case '\r':    /* CR and LF are equivalent */
  97.         case '\n':
  98.             *cp++ = '\r';
  99.             *cp++ = '\n';
  100.             printf("\n");
  101.             cnt = cp - linebuf;
  102.             cp = linebuf;
  103.             break;
  104.         case RUBOUT:
  105.         case '\b':        /* Backspace */
  106.             if(cp != linebuf){
  107.                 cp--;
  108.                 if (ttyecho)
  109.                     printf("\b \b");
  110.             }
  111.             break;
  112.         case CTLR:    /* print line buffer */
  113.             if(ttyecho)
  114.                 printf("^R");
  115.             printf("\n");
  116.             if(ttyecho) {
  117.                 rp = linebuf ;
  118.                 while (rp < cp)
  119.                     putchar(*rp++) ;
  120.             }
  121.             break ;
  122.         case CTLU:    /* Line kill */
  123.             if(ttyecho) {
  124.                 while(cp != linebuf){
  125.                     cp--;
  126.                     printf("\b \b");
  127.                 }
  128.             } else
  129.                 cp = linebuf;
  130.             break;
  131.         case CTLV:
  132.             ttymode = TTY_LIT;
  133.             break;
  134.         case CTLW:    /* erase word */
  135.             seenprint = 0 ;    /* we haven't seen a printable char yet */
  136.             while (cp != linebuf) {
  137.                 cp--;
  138.                 if(ttyecho)
  139.                     printf("\b \b") ;
  140.                 if (isspace(*cp)) {
  141.                     if (seenprint)
  142.                         break ;
  143.                 }
  144.                 else
  145.                     seenprint = 1 ;
  146.             }
  147.             break ;
  148.         default:    /* Ordinary character */
  149.             *cp++ = c;
  150. #ifndef    AMIGA
  151.             /* ^Z apparently hangs the terminal emulators under
  152.              * DoubleDos and Desqview. I REALLY HATE having to patch
  153.              * around other people's bugslike this!!!
  154.              */
  155.             if (ttyecho && (c != CTLZ))
  156.                 putchar(c);
  157. #endif
  158.             if(cp >= &linebuf[LINESIZE]){
  159.                 cnt = cp - linebuf;
  160.                 cp = linebuf;
  161.             }
  162.             break;
  163.         }
  164.     }
  165.     if(cnt > 0)
  166.         *buf = linebuf;
  167.     else
  168.         *buf = NULLCHAR;
  169. #ifdef    FLOW
  170.     if(cp > linebuf)
  171.         ttyflow = 0;
  172.     else
  173.         ttyflow = 1;
  174. #endif
  175.     fflush(stdout);
  176.     return cnt;
  177. }
  178.